import nodemailer from "nodemailer";
import dayjs from "dayjs";
// 邮箱验证码key
export const emailKey = (email: string) => "email-lineo" + email;
// 创建传输对象
const transporter = nodemailer.createTransport({
    host: "smtp.qq.com",
    port: 587,
    secure: false,
    auth: {
        user: "1445154800@qq.com", //我的邮箱
        pass: "mhzcjiztvzxzjjfi", //授权码
    },
});
const from = "1445154800@qq.com";
// 导出发送邮件方法
export async function sendEMail(user: { username: string; email: string }, code: number | string) {
    const { username, email } = user;
    // 发送邮件
    const info = transporter.sendMail(
        {
            from: from, //发送人邮箱
            to: email, //发送给谁
            subject: `你好，亲爱的${username}`, //标题
            text: `你好，亲爱的${username}，您于${dayjs().format("YYYY-MM-DD hh:mm:ss")}在demo平台获取的验证码是：${code}，请尽快验证！`,
        },
        (error, info) => {
            if (error) {
                return error;
            }
            return info;
        }
    );

    console.log("邮件发送成功：", info);
}
